function [clmatrix,small_corr_idx]=clfilter(clmatrix,clcorr,tol)
%
% Filter the common-lines matrix so that only common-lines with high
% correlations are retained. Only dissimilarities that are smaller than tol
% are kept.
%
% The functions returns the new common_lines matrix and the indices of the
% rows in the original matrix used to form the filtered matrix.

% Yoel Shkolnisky, October 2007.

if nargin<3
    tol=1000;
end

idx1=find(clcorr>0);         %ignore zero entries in the matrix
[v,idx2]=sort(clcorr(idx1)); % sort these non-zero values
nzp=length(find(v<tol))/length(v); % percentage of non-zero correlations to retain
nnz= round(length(idx1)*nzp);           % number of non-zero correlations to retain
large_corr_idx=idx1(idx2(nnz+1:end));
small_corr_idx=idx1(idx2(1:nnz));
clmatrix(large_corr_idx)=0; % zero all pairs( k1,k2) with large correlations

if ndims(clmatrix)==2
    [I,J]=ind2sub(size(clmatrix),large_corr_idx);
    large_corr_idx=sub2ind(size(clmatrix),J,I);
else
    [I,J,K]=ind2sub(size(clmatrix),large_corr_idx);
    large_corr_idx=sub2ind(size(clmatrix),J,I,K);
end
clmatrix(large_corr_idx)=0; % zero all pairs( k2,k1) with large correlations